home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / Src / Ch3 / Custom.frm (.txt) < prev    next >
Visual Basic Form  |  1999-03-31  |  7KB  |  216 lines

  1. VERSION 5.00
  2. Begin VB.Form frmCustom 
  3.    Caption         =   "Custom"
  4.    ClientHeight    =   3270
  5.    ClientLeft      =   2055
  6.    ClientTop       =   1320
  7.    ClientWidth     =   5415
  8.    LinkTopic       =   "Form1"
  9.    PaletteMode     =   1  'UseZOrder
  10.    ScaleHeight     =   218
  11.    ScaleMode       =   3  'Pixel
  12.    ScaleWidth      =   361
  13.    Begin VB.HScrollBar hbarRed 
  14.       Height          =   255
  15.       LargeChange     =   16
  16.       Left            =   1020
  17.       Max             =   255
  18.       TabIndex        =   4
  19.       Top             =   2160
  20.       Width           =   4275
  21.    End
  22.    Begin VB.HScrollBar hbarGreen 
  23.       Height          =   255
  24.       LargeChange     =   16
  25.       Left            =   1020
  26.       Max             =   255
  27.       TabIndex        =   3
  28.       Top             =   2520
  29.       Width           =   4275
  30.    End
  31.    Begin VB.HScrollBar hbarBlue 
  32.       Height          =   255
  33.       LargeChange     =   16
  34.       Left            =   1020
  35.       Max             =   255
  36.       TabIndex        =   2
  37.       Top             =   2880
  38.       Width           =   4275
  39.    End
  40.    Begin VB.PictureBox picCustom 
  41.       AutoRedraw      =   -1  'True
  42.       Height          =   1575
  43.       Left            =   2760
  44.       Picture         =   "Custom.frx":0000
  45.       ScaleHeight     =   101
  46.       ScaleMode       =   3  'Pixel
  47.       ScaleWidth      =   165
  48.       TabIndex        =   1
  49.       Top             =   120
  50.       Width           =   2535
  51.    End
  52.    Begin VB.PictureBox picDefault 
  53.       AutoRedraw      =   -1  'True
  54.       Height          =   1575
  55.       Left            =   120
  56.       ScaleHeight     =   1515
  57.       ScaleWidth      =   2475
  58.       TabIndex        =   0
  59.       Top             =   120
  60.       Width           =   2535
  61.    End
  62.    Begin VB.Label Label1 
  63.       Caption         =   "Red"
  64.       Height          =   255
  65.       Index           =   0
  66.       Left            =   120
  67.       TabIndex        =   12
  68.       Top             =   2160
  69.       Width           =   495
  70.    End
  71.    Begin VB.Label Label1 
  72.       Caption         =   "Green"
  73.       Height          =   255
  74.       Index           =   1
  75.       Left            =   120
  76.       TabIndex        =   11
  77.       Top             =   2520
  78.       Width           =   495
  79.    End
  80.    Begin VB.Label Label1 
  81.       Caption         =   "Blue"
  82.       Height          =   255
  83.       Index           =   2
  84.       Left            =   120
  85.       TabIndex        =   10
  86.       Top             =   2880
  87.       Width           =   495
  88.    End
  89.    Begin VB.Label Label1 
  90.       Alignment       =   2  'Center
  91.       Caption         =   "Default Palette"
  92.       Height          =   255
  93.       Index           =   3
  94.       Left            =   120
  95.       TabIndex        =   9
  96.       Top             =   1800
  97.       Width           =   2535
  98.    End
  99.    Begin VB.Label lblRed 
  100.       BorderStyle     =   1  'Fixed Single
  101.       Height          =   255
  102.       Left            =   600
  103.       TabIndex        =   8
  104.       Top             =   2160
  105.       Width           =   375
  106.    End
  107.    Begin VB.Label lblGreen 
  108.       BorderStyle     =   1  'Fixed Single
  109.       Height          =   255
  110.       Left            =   600
  111.       TabIndex        =   7
  112.       Top             =   2520
  113.       Width           =   375
  114.    End
  115.    Begin VB.Label lblBlue 
  116.       BorderStyle     =   1  'Fixed Single
  117.       Height          =   255
  118.       Left            =   600
  119.       TabIndex        =   6
  120.       Top             =   2880
  121.       Width           =   375
  122.    End
  123.    Begin VB.Label Label1 
  124.       Alignment       =   2  'Center
  125.       Caption         =   "Custom Palette"
  126.       Height          =   255
  127.       Index           =   5
  128.       Left            =   2760
  129.       TabIndex        =   5
  130.       Top             =   1800
  131.       Width           =   2535
  132.    End
  133. Attribute VB_Name = "frmCustom"
  134. Attribute VB_GlobalNameSpace = False
  135. Attribute VB_Creatable = False
  136. Attribute VB_PredeclaredId = True
  137. Attribute VB_Exposed = False
  138. Option Explicit
  139. Private Type PALETTEENTRY
  140.     peRed As Byte
  141.     peGreen As Byte
  142.     peBlue As Byte
  143.     peFlags As Byte
  144. End Type
  145. Private Declare Function GetDeviceCaps Lib "gdi32" (ByVal hdc As Long, ByVal nIndex As Long) As Long
  146. Private Declare Function ResizePalette Lib "gdi32" (ByVal hPalette As Long, ByVal nNumEntries As Long) As Long
  147. Private Declare Function SetPaletteEntries Lib "gdi32" (ByVal hPalette As Long, ByVal wStartIndex As Long, ByVal wNumEntries As Long, lpPaletteEntries As PALETTEENTRY) As Long
  148. Private Declare Function RealizePalette Lib "gdi32" (ByVal hdc As Long) As Long
  149. Private Const RASTERCAPS = 38
  150. Private Const RC_PALETTE = &H100
  151. ' Resize picCustom's palette so it has only one
  152. ' entry. We will use that entry to display the
  153. ' color selected by the scroll bars.
  154. Private Sub ShrinkPalette()
  155.     If ResizePalette(picCustom.Picture.hPal, 1) = 0 Then
  156.         MsgBox "Error resizing palette."
  157.     End If
  158. End Sub
  159. ' Display the selected RGB value in all picture
  160. ' boxes.
  161. Private Sub UpdateColors()
  162. Dim r As Integer
  163. Dim g As Integer
  164. Dim b As Integer
  165. Dim palentry As PALETTEENTRY
  166.     r = hbarRed.Value
  167.     g = hbarGreen.Value
  168.     b = hbarBlue.Value
  169.     ' Update the numeric labels.
  170.     lblRed.Caption = Format$(r)
  171.     lblGreen.Caption = Format$(g)
  172.     lblBlue.Caption = Format$(b)
  173.     ' Display the color in the default picture.
  174.     picDefault.Line (0, 0)-Step(picDefault.ScaleWidth, picDefault.ScaleHeight), RGB(r, g, b), BF
  175.     ' Put the new color in the custom palette.
  176.     palentry.peRed = r
  177.     palentry.peGreen = g
  178.     palentry.peBlue = b
  179.     If SetPaletteEntries(picCustom.Picture.hPal, 0, 1, palentry) = 0 Then
  180.         MsgBox "Error updating palette entry."
  181.     End If
  182.     ' Make the change take effect.
  183.     RealizePalette picCustom.hdc
  184.     ' Fill the custom palette picture.
  185.     picCustom.Line (0, 0)-Step(picCustom.ScaleWidth, picCustom.ScaleHeight), RGB(r, g, b) + &H2000000, BF
  186. End Sub
  187. Private Sub hbarBlue_Change()
  188.     UpdateColors
  189. End Sub
  190. Private Sub hbarBlue_Scroll()
  191.     UpdateColors
  192. End Sub
  193. Private Sub Form_Load()
  194.     ' Make sure the screen supports palettes.
  195.     If Not GetDeviceCaps(hdc, RASTERCAPS) And RC_PALETTE Then
  196.         MsgBox "This system is not using palettes.", vbCritical
  197.         End
  198.     End If
  199.     ' Load the system palette.
  200.     ShrinkPalette
  201.     ' Display the initial color (black).
  202.     UpdateColors
  203. End Sub
  204. Private Sub hbarGreen_Change()
  205.     UpdateColors
  206. End Sub
  207. Private Sub hbarGreen_Scroll()
  208.     UpdateColors
  209. End Sub
  210. Private Sub hbarRed_Change()
  211.     UpdateColors
  212. End Sub
  213. Private Sub hbarRed_Scroll()
  214.     UpdateColors
  215. End Sub
  216.